const config = require('../config')
let {HttpException} = require('../core/http-exception')
const { logger } = require('../config/logs');
const catchError = async (ctx,next)=>{
    try {
        await next()
    } catch (error) {
        const env = config.env == 'dev'
        const isHttpException = error instanceof HttpException
        if(env && !isHttpException){
            throw error
        }
        if(isHttpException){
            // ctx.body = { //抛出特定异常
            //     msg:error.msg,
            //     code:error.errCode,
            // }
            ctx.body = error // 直接抛出全部异常，方便设置请求返回值
            ctx.status = error.code
        }else{
            ctx.body = {
                msg:'服务器出现问题了',
                code:50000
            }
            //错误日志处理程序
            logger.error(error);
            ctx.status = 500
        }
    }
}
module.exports = catchError